package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class Deletebook implements Operate{
    @Override
    public void work(BookList books) {
        System.out.println("删除书籍...");
        System.out.print("请输入你要删除的书籍:--->");
        Scanner scanner=new Scanner(System.in);
        String name =scanner.nextLine();
        int count=books.getUsedbooks();
        int index=-1;
        boolean flag=false;
        for (int i = 0; i <count ; i++) {
            if(books.getbook(i).getName().equals(name)){
               index=i;
               flag=true;
               break;
            }
        }
        if(flag==false) {
            System.out.println("没有找到你要删除的书籍-----");
            System.out.println();
            return ;
        }
        if (books.getbook(index).isIsborrow()==true){
            System.out.println("该书本已经被借出，暂时不能进行操作-----");
            System.out.println();
            return;
        }else{
            for(int j=index;j<count-1;j++)
            {
                books.setBooks(j,books.getbook(j+1));
            }
            System.out.println("删除成功------->");
            books.setUsedbooks(count-1);
            books.setBooks(count-1,null);
        }
    }
}
